package com.scau.database.service.salesperson;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scau.data.entity.*;
import com.scau.database.common.service.ImportMapperService;
import com.scau.database.entity.salesperson.AfterSaleCenterQuery;
import com.scau.database.entity.salesperson.SalesInfoItemVO;
import com.scau.database.entity.salesperson.SalesInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

/**
 * @Author: x1aolone
 * @Date: 2020/5/9 19:55
 */
@Service
public class SalesInfoService {

    @Autowired
    public ImportMapperService importMapperService;

    /**
     * 通过小票流水获取订单信息
     * @param shopId 小票流水
     * @return
     */
    public SalesInfoVO getSalesInfo(Long shopId) {
        SalesInfoVO VO = new SalesInfoVO();
        // 先通过流水号获取流水号、店铺id、总价、时间
        LambdaQueryWrapper<ShopDO> salesInfoQuery =
                Wrappers.<ShopDO>lambdaQuery()
                        .eq(ShopDO::getId, shopId)
                        .last(" limit 1");
        ShopDO shopDO = importMapperService.shopMapper.selectOne(salesInfoQuery);
        if (shopDO == null) {
            return null;
        }
        VO.setShopId(shopDO.getId());
        VO.setStoreId(shopDO.getStoreId());
        VO.setTotalPrice(shopDO.getTotalPrice());
        VO.setTime(LocalDateTime.ofInstant(shopDO.getTime().toInstant(), ZoneId.systemDefault()));
        // 再通过店铺id获取店铺名
        LambdaQueryWrapper<StoreDO> storeInfoQuery =
                Wrappers.<StoreDO>lambdaQuery()
                        .eq(StoreDO::getId, VO.getStoreId())
                        .last(" limit 1");
        StoreDO storeDO = importMapperService.storeMapper.selectOne(storeInfoQuery);
        if (storeDO == null) {
            return null;
        }
        VO.setStoreName(storeDO.getName());
        // 然后通过流水号获取商品列表，同时需要根据商品barCode从获取商品名、店铺三包、厂家三包
        List<SalesInfoItemVO> itemVOS = importMapperService.shopLineMapper.getSalesInfoItemsByShopId(VO.getShopId());
        if (itemVOS == null) {
            return null;
        }
        VO.setItems(itemVOS);
        return VO;
    }

    /**
     * 记录第三方维修点
     * @param DO 第三方维修点信息
     * @return 插入数据库受影响行数
     */
    public Integer recordAfterSaleCenter(RepairStoreDO DO) {

        return importMapperService.repairStoreMapper.insert(DO);
    }

}
